package com.wwinsoft.oa.web.plan;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.wwinsoft.framework.pojo.security.UserBean;
import com.wwinsoft.framework.service.ServiceException;
import com.wwinsoft.framework.service.system.CodeManager;
import com.wwinsoft.framework.utils.JXLTHelper;
import com.wwinsoft.framework.web.CrudActionSupport;
import com.wwinsoft.modules.orm.Page;
import com.wwinsoft.oa.entity.plan.Workplan;
import com.wwinsoft.oa.pojo.plan.WorkplanBean;
import com.wwinsoft.oa.service.plan.WorkplanService;

@Namespace("/plan")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "workplan.action", type = "redirect"),
                 @Result(name = "reinput", location = "workplan!input.action", type = "redirect") })
public class WorkplanAction extends CrudActionSupport<Workplan> {

    @Autowired
    private WorkplanService workPlanService;

    @Autowired
    private CodeManager codeManager;

    private Long id;
    private Workplan entity;
    private WorkplanBean filter = new WorkplanBean();
    private Page page = new Page();

    private static final String WORKPLAN_EXCEL_TEMPLATE = "/template/excel/Workplan_Template.xls";
    private static final String USER_NOPLAN_EXCEL_TEMPLATE = "/template/excel/User_NoPlan_Template.xls";


    public void setId(Long id) {
        this.id = id;
    }

    public Workplan getModel() {
        return entity;
    }

    public Page<Workplan> getPage() {
        return page;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = workPlanService.getWorkPlan(id);
        } else {
            entity = new Workplan();
        }
    }

    //-- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        page = workPlanService.searchWorkPlanOwned(page, filter);
        return SUCCESS;
    }

    public void listExp() throws Exception{
        List<Workplan> workplans = workPlanService.searchWorkPlanOwned(filter);
        exportExcel(workplans);
    }

    public String subordinate() throws Exception {
        page = workPlanService.searchWorkPlanSubordinate(page, filter);
        return "subordinate";
    }

    public void subordinateExp() throws Exception {
        List<Workplan> workplans = workPlanService.searchWorkPlanSubordinate(filter);
        exportExcel(workplans);
    }
    
    public String noPlan() throws Exception{
        page = workPlanService.searchUserNoPlanPage(page, filter);
        return   "noPlanUsers";
    }

    public void noPlanExp() throws Exception{
        List<UserBean> userBeanList = workPlanService.searchUserNoPlanList(filter);
        Map<String, Object> beans = new HashMap<String, Object>();
        beans.put("dataList", userBeanList);
        String fileName = "未写工作计划下属";
        JXLTHelper.writeXLTtoResponse(beans, USER_NOPLAN_EXCEL_TEMPLATE, fileName);
    }

    private void exportExcel(List<Workplan> workplans) {
        Map<String, Object> beans = new HashMap<String, Object>();
        beans.put("dataList", workplans);
        beans.put("codeManager", codeManager);
        String fileName = "工作日志";
        JXLTHelper.writeXLTtoResponse(beans, WORKPLAN_EXCEL_TEMPLATE, fileName);
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String save() throws Exception {
    	if (entity != null && "2".equals(entity.getStatus())) {
    		entity.setEndTime(new Date());
    	} else {
    		entity.setEndTime(null);
    	}
        workPlanService.saveWorkPlan(entity);
        addActionMessage("保存工作计划成功");
        return RELOAD;
    }

    @Override
    public String delete() throws Exception {
        try {
            workPlanService.deleteWorkPlan(id);
            addActionMessage("删除工作计划成功");
        } catch (ServiceException e) {
            logger.error(e.getMessage(), e);
            addErrorMessage(e.getMessage());
        }
        return RELOAD;
    }

    public void prepareView()throws  Exception{
        prepareModel();
    }

    public String view() throws Exception{
        return "view";
    }

    public String saveAndInput() throws Exception {
        save();
        return "reinput";
    }

    public void prepareSaveAndInput() throws Exception {
        prepareModel();
    }

    public WorkplanBean getFilter() {
        return filter;
    }

    public void setFilter(WorkplanBean filter) {
        this.filter = filter;
    }
}
